package com.bitdf.txing.product.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.product.entity.ProductAttrValueEntity;
import com.bitdf.txing.product.service.AttrGroupService;
import com.bitdf.txing.product.service.AttrService;
import com.bitdf.txing.product.service.ProductAttrValueService;
import com.bitdf.txing.product.vo.AttrRespVo;
import com.bitdf.txing.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author lizhiwei
 * @email sunlightcs@gmail.com
 * @date 2022-05-16 20:22:24
 */
@RestController
@RequestMapping("${txing.request.path.product}/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    @SaCheckLogin
    @SaCheckPermission("mall:product:spu:attr:value:update")
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities)
    {
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    ///product/attr/info/{attrId}

    // /product/attr/base/listforspu/{spuId}
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);

        return R.ok().put("data",entities);
    }


    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId")Long attrId)
    {
        AttrRespVo respVo=attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }
    /**
     * 列表
     */
//    @RequestMapping("/list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = attrService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }


    /**
     * 信息
     */
//    @RequestMapping("/info/{attrId}")
//    public R info01(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
//
//        return R.ok().put("attr", attr);
//    }

    /**
     * 保存
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:add")
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 更新属性
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:update")
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAtte(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:delete")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }



    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType") String type)
    {
        PageUtils pageUtils = attrService.queryBaseAttrPage(params, catelogId,type);
        return R.ok().put("page", pageUtils);
    }


}
